package org.one.stone.soup.grfxML;

import java.util.Vector;

import org.one.stone.soup.mjdb.data.field.ArrayField;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataStates extends ArrayField implements IArray{
	public static String DEFAULT_ID="_States";
public DataStates() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void add(DataState data,Object source) throws LockException
{
	_add(data,source);
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
	  element = xml.getElementByIndex(loop);

	 if(element!=null)
	 {
	  try{
		Class clazz  = Class.forName( NameHelper.toClassName(element.getName()) );
		DataState newItem = (DataState)clazz.newInstance();
		clazz.
			getMethod(NameHelper.BUILD_METHOD,NameHelper.BUILD_SIG).
				invoke(newItem,new Object[]{element});
		add(newItem,this);
	  }
	  catch(ClassNotFoundException ce)
	  {
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{element.getName(),"store",""+element.getStartLineNo()} );
	  }
	  catch(Exception e)
	  {
		e.printStackTrace();
	  }// Major exception handleing needed here (and in this function)
	 }

	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	int loop;
	for(loop=0;loop<xml.getElementCount();loop++)
	{
	  element = xml.getElementByIndex(loop);

	 if(element!=null)
	 {
	  if(loop<size())
	  {
		  get(loop).copyFromgrfxML( element );
	  }
	  else
	  {
		  try{
			Class clazz  = Class.forName( NameHelper.toClassName(element.getName()) );
			DataState newItem = (DataState)clazz.newInstance();
			clazz.
				getMethod(NameHelper.BUILD_METHOD,NameHelper.BUILD_SIG).
					invoke(newItem,new Object[]{element});
			add(newItem,this);
		  }
		  catch(ClassNotFoundException ce)
		  {
				GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{element.getName(),"store",""+element.getStartLineNo()} );
		  }
		  catch(Exception e)
		  {
			e.printStackTrace();
		  }// Major exception handleing needed here (and in this function)
	  }
	 }
	}

	while(loop<size())
	{
		try{
			remove( size()-1,this );
		}catch(LockException le){}
	}
}
public DataState get(int index)
{
	return (DataState)_get(index);
}
public DataStates getClone() {

	DataStates newStates = new DataStates();

	for (int loop = 0; loop < size(); loop++) {
		try {
			newStates.add(((DataState) get(loop)).getClone(), this);
		} catch (LockException le) {
		} // dummy catch
	}

	return newStates;
}
public DataStates getClone(String path) {

	DataStates newStates = new DataStates();

	GrfxMLStoreProcessor.setCloneName( path,this,newStates );

	for(int loop=0;loop<size();loop++)
	{
	  try{
		newStates.add(
				((DataState)get(loop)).getClone(path),
				this
			);
	  }
	  catch(LockException le){}// dummy catch
	}

	return newStates;
}
public Vector getClonedElements()
{
	Vector elements = super.getClonedElements();

	return elements;
}
public Vector getClonedElements(String path)
{
	Vector elements = super.getClonedElements(path);

	return elements;
}
public String getMemberName() {
	return Token.STATE;
}
public String getValue(String name) {
	return null;
}
public void insert(DataState data,int index,Object source) throws LockException
{
	_insert(data,index,source);
}
public void register(DataLinker store) {
	super.register(store);
}
public void remove(int index,Object source) throws LockException
{
	_remove(index,source);
}
public void remove(DataState data,Object source) throws LockException
{
	_remove(data,source);
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj,newObj,false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	for(int loop=0;loop<size();loop++)
	{
		if(get(loop)==oldObj)
		{
			try{
				set((DataState)newObj,loop,this);
			}
			catch(LockException le){}//dummy catch
		}
		get(loop).replace(oldObj,newObj,recursive);
	}

}
public void resetChanged() {
	super.resetChanged();
}
public void set(DataState data,int index,Object source) throws LockException
{
	_set(data,index,source);
}
public String togrfxML(String name) {
	String xml = "<" + name + ">";

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	for (int loop = 0; loop < size(); loop++) {
		try {
			xml += get(loop).togrfxML(NameHelper.systemToTagName((String) get(loop).getClass().getField("DEFAULT_ID").get(get(loop))));
		} catch (Exception e) {
		} //dummy catch

	}
	xml += "</" + name + ">\n";

	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
